// 玩家手牌
class playHandPokerVideo extends egret.Sprite{
    public pointId:number;//保存位置ID
    private SeatType:number;
    private cardNum:number;
    public constructor(_seatType:number=1){
        super();
        this.SeatType = _seatType;//座位类型
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddEd,this);
    }
    private onAddEd(){
        this.addChild(this.winPokerBg);
        this.addChild(this.handPokerBg);
        this.once(egret.Event.REMOVED_FROM_STAGE,this.onRemove,this);
    }
    private onRemove(){
    }

    private handPokerBg:egret.Sprite = new egret.Sprite();//手牌容器
    private winPokerBg:egret.Sprite = new egret.Sprite();//赢牌容器
    private handPokerList:Array<any> = new Array();
    private winPokerList:Array<any> = new Array();
    private getPoker:any;//摸的
    private winPokerX:number;
    private winPokerY:number;
    //右边
    private rightOrientation(){
        let lslength:number = this.handPokerList.length;
        this.handPokerList.reverse();//倒序排序
        for(var i=0;i<lslength;i++){
            this.handPokerBg.addChild(this.handPokerList[i].objmc);
            this.handPokerList[i].objmc.y = 30*i;
        }

        for(var i=this.winPokerList.length;i>0;i--){
            this.winPokerList[i-1].y = (104*i);
            this.winPokerList[i-1].scaleX = this.winPokerList[i-1].scaleY = 1.2;
        }
        this.winPokerBg.y = this.handPokerBg.y +(30*(lslength-1))-40;

        if(this.getPoker){
            this.getPoker.y = this.handPokerBg.y - 50;
        }    
        this.handPokerList.reverse();//倒序排序
    }
    //对面
    private topOrientation(){
        this.handPokerList.reverse();//倒序排序
        for(var i=0;i<this.winPokerList.length;i++){
            this.winPokerList[i].x = (118*i);
            this.winPokerList[i].scaleX = this.winPokerList[i].scaleY = 1.2;
        }
        
        let lslength:number = this.handPokerList.length;
        for(var i=0;i<lslength;i++){
            this.handPokerBg.addChild(this.handPokerList[i].objmc);
            this.handPokerList[i].objmc.x = 39*i;
        }

        this.winPokerBg.x = (39*lslength)+10;
        this.winPokerBg.y = 0;

        if(this.getPoker){
            this.getPoker.x =-(this.getPoker.width + 10);
        }
        this.handPokerList.reverse();//倒序排序
    }
    //左边
    private leftOrientation(){
        this.winPokerBg.height  = 0;
        for(var i=0;i<this.winPokerList.length;i++){
            this.winPokerList[i].y = (104*i);
            this.winPokerBg.height  = (104*(i+1));
            this.winPokerList[i].scaleX = this.winPokerList[i].scaleY = 1.2;
        }
        this.winPokerBg.y = 0;

        for(var i=0;i<this.handPokerList.length;i++){
            this.handPokerBg.addChild(this.handPokerList[i].objmc);
            this.handPokerList[i].objmc.y = 30*i;
            this.handPokerBg.height = 30*i;
        }
        this.handPokerBg.y = this.winPokerBg.y+this.winPokerBg.height+5;
        
        if(this.getPoker){
            this.getPoker.y = (30*(i))+5;
            //this.getPoker.y = this.handPokerBg.y+this.handPokerBg.height;
        }
    }

    private handPokerData:Array<any> = new Array();
    /*添加手牌*/
    public setContent(amount:number,lastGetCard=null,msg=null){
        
        this.cardNum = amount;
        switch(this.SeatType){
            case 1:
            this.addhandPoker("gamePoker_2_png",amount,lastGetCard,msg);
            this.rightOrientation();
            break;

            case 2:
            this.addhandPoker("gamePoker_3_png",amount,lastGetCard,msg);
            this.topOrientation();
            break;

            case 3:
            this.addhandPoker("gamePoker_4_png",amount,lastGetCard,msg);
            this.leftOrientation();
            break;
        }
    }
    public addhandPoker(picName:string,amount:number,lastGetCard,msg=null){ 
        if (!msg) {
            return 
        }
        this.cardNum = amount;
        this.handPokerList = [];
        //this.winPokerList = [];
        for(var i=this.handPokerBg.numChildren;i>0;i--){
            this.handPokerBg.removeChildAt(i-1);
        }
        this.getPoker = null;
        this.getPoker = new handPokerPlay(this.SeatType);
        this.getPoker.setContent(msg[i].suitType+"_"+msg[i].count+"_png");
        this.getPoker.visible = false;
        this.handPokerBg.addChild(this.getPoker);

        this.handPokerList = [];
        let lslast:any;
        for(var i:number=0;i<msg.length;i++){
            if(lastGetCard&&!lslast&&msg[i].cardIndex==lastGetCard.cardIndex){
                lslast = lastGetCard;
                //this.cardMove.visible = true;
                //alert("aa");
            }else{
                this.addSinglePoker(msg[i].suitType,msg[i].count,msg[i].cardIndex,false);
            } 
        }      
            this.pokerArrayAscending();
        if(lslast&&lastGetCard){
            this.addSinglePoker(lastGetCard.suitType,lastGetCard.count,lastGetCard.cardIndex,true);
        } 
    }

    //牌值排序
    public pokerArrayAscending(){         
        var lsArray:Array<any> = this.handPokerList.slice();
        var newArray:Array<any> = new Array();//排列红中
        var i:number=lsArray.length-1, j:number, temp:any;
        for(var m:number=0; m<lsArray.length; m++){  
            for(var n:number=lsArray.length-1; n > m; n--){  
                 if(lsArray[n].data.id!=32&&lsArray[n-1].data.id > lsArray[n].data.id){
                     temp = lsArray[n-1];         
                     lsArray[n-1] = lsArray[n];                                 
                     lsArray[n] = temp;
                 }  
            }  
        }  
        this.handPokerList = lsArray;
    }
    /*接一张牌*/
    private pokerNum:number = 0;//添加过的牌数量
    private hideCred:any;
    public addSinglePoker(flower:string,value:string,id:number,grab:boolean=false){
       /*if(this.getPoker){
           this.getPoker.setContent(flower+"_"+value+"_png");
           StageData.soundObj.soundSinglePlay("getCard");
           //this.getPoker.visible = true;    
       }*/
       
       if(flower&&value&&id){     
            let pokerMc:handPokerPlay = new handPokerPlay(this.SeatType); 
            pokerMc.name = "poker_"+this.pokerNum;//this.handPokerList.length;
            this.pokerNum ++;
            pokerMc.setContent(flower+"_"+value+"_png");
            this.handPokerBg.addChild(pokerMc);
            
            if(grab){
                if(!this.getPoker){
                    this.getPoker = null;
                    this.getPoker = new handPokerPlay(this.SeatType);
                    this.getPoker.visible = false;
                    this.handPokerBg.addChild(this.getPoker);
                }
                this.getPoker.visible = true;
                this.getPoker.setContent(flower+"_"+value+"_png");
                pokerMc.visible = false;
                this.hideCred = pokerMc;
                //pokerMc.x = pokerMc.width*this.handPokerList.length+10;
                //this.handPokerBg.width = pokerMc.width*(this.handPokerList.length+1);
                this.handPokerList.push({objmc:pokerMc,data:{flower:flower,value:value,id:id}});
                this.updateSize();
            }else{                
                //pokerMc.x = pokerMc.width*this.handPokerList.length;
                //this.handPokerBg.width = pokerMc.width*(this.handPokerList.length+1);                
                this.handPokerList.push({objmc:pokerMc,data:{flower:flower,value:value,id:id}});
            }
        }
    }
    /*打一张牌*/
    public deleteSinglePoker(id:number){
       /*if(this.getPoker){
           if(this.getPoker.visible == true){
               this.getPoker.visible = false;
           }else{
               this.deletePoker(1);
           }
       }*/
       if(!id) return;
       this.getPoker.visible = false;
       if(this.hideCred){
           this.hideCred.visible = true;
           this.hideCred = null;
       }
        for(var i=0;i<this.handPokerList.length;i++){
            if(this.handPokerList[i].data.id == id){
                this.handPokerBg.removeChild(this.handPokerList[i].objmc);
                this.handPokerList.splice(i,1);
                //刷新牌排列位置
               // this.pokerSort();
                this.pokerArrayAscending();
                this.updateSize();
                break;
            }
        }
        /*for(var i=0;i<this.handPokerList.length;i++){
              this.handPokerList[i].objmc.y = 0;
        }    */
       this.dispatchEventWith(PokerEvent.OUT_POKER,false,
                {x:this.getPoker.x,y:this.getPoker.y,doubleClick:true,value:null});
    }
    /*删除单张或多张牌*/
    public deletePoker(amount:number=1,cardId:number=-1){
        if(amount>this.handPokerList.length-1){
           amount=this.handPokerList.length-1;
        }
        console.log("\n\n\ndddd:"+this.handPokerList.length+":"+amount);
        for(var i=0;i<amount;i++){
            this.handPokerBg.removeChild(this.handPokerList[0].objmc);
            this.handPokerList.splice(0,1);
        }
        /*if(cardId>-1){

        }
        //*/this.updateCardValue();
        this.updateSize();
    }
    //重新刷新牌值
    private updateCardValue(){
        for(var i=0;i<this.handPokerList.length;i++){
            this.handPokerList[i].objmc.setContent(this.handPokerList[i].data.suitType+"_"+this.handPokerList[i].data.count+"_png");
        }
    }
     //清除所有牌
    public clearAllPoker(){
        for(var i=this.handPokerBg.numChildren;i>0;i--){
            this.handPokerBg.removeChildAt(i-1);
        }
        for(var i=this.winPokerBg.numChildren;i>0;i--){
            this.winPokerBg.removeChildAt(i-1);
        }
        this.handPokerList = [];
        this.winPokerList = [];
        if(this.getPoker){
           this.getPoker.visible = false;
       }
    }
    /*刷新手牌位置*/
    private updateSize(){
        switch(this.SeatType){
            case 1:
            this.rightOrientation();
            break;

            case 2:
            this.topOrientation();
            break;

            case 3:
            this.leftOrientation();
            break;
        }
        //this.getPoker.visible = false;
    }

    //删除一张牌
    /*public deleteSinglePoker(id:number){
        this.cardMove.visible = false;
        if(!id) return;
        for(var i=0;i<this.handPokerList.length;i++){
            if(this.handPokerList[i].data.id == id){
                this.handPokerBg.removeChild(this.handPokerList[i].objmc);
                this.handPokerList.splice(i,1);
                //刷新牌排列位置
                this.pokerSort();
                this.onStageSize();
                break;
            }
        }
        for(var i=0;i<this.handPokerList.length;i++){
              this.handPokerList[i].objmc.y = 0;
        }        
    }*/

    //删除相同指定的手牌
    public deleteAppointPoker(flower:string,value:string,num:number = 9999){
        console.log('deleteAppointPoker', flower, value, num);
        if(flower&&value){
            var newArr:Array<any>=new Array();  
            var deleteNumber:number = 0;          
             for(var i=0;i<this.handPokerList.length;i++){
                if(this.handPokerList[i].data.flower == flower&&this.handPokerList[i].data.value == value){
                    if(deleteNumber<num){
                        this.handPokerBg.removeChild(this.handPokerList[i].objmc);
                        deleteNumber ++;                        
                    }else{
                        newArr.push(this.handPokerList[i]);
                    }
                }else{
                    //刷新牌排列位置
                    newArr.push(this.handPokerList[i]);           
                }
            }
            this.handPokerList = newArr;
        }
        this.updateSize(); 
        this.pokerArrayAscending();
    }

    // //刷新赢牌列表-碰牌/杠(花色，值，ID，赢牌的碰杠类型)
    // public winPokerUpdateList(pokerList:Array<any>){        
    //     for(var i=0;i<this.winPokerList.length;i++){
    //         this.winPokerBg.removeChild(this.winPokerList[i]);
    //     }
    //     this.winPokerList = [];
    //     this.winPokerBg.width = 0;
    //     for(var i=0;i<pokerList.length;i++){
    //         this.winPoker(pokerList[i].actionCard.suitType,pokerList[i].actionCard.count,pokerList[i].actionCode,GameData.winPokerCode[pokerList[i].actionCode]);
    //      }
    // }

    private lastWinPokerList = []
    //刷新赢牌列表-碰牌/杠(花色，值，ID，赢牌的碰杠类型)
    public winPokerUpdateList(pokerList:Array<any>){  
        const hasAngang = pokerList.some(poker => poker.actionType === 'AN_GANG')
        if (_.isEqual(this.lastWinPokerList, pokerList) && !hasAngang) {
            console.log('winPokerUpdateList isEqual');
            return 
        } else {
            this.lastWinPokerList = pokerList
        }
        for(var i=this.winPokerBg.numChildren;i>0;i--){
            this.winPokerBg.removeChildAt(i-1);
        }
        this.winPokerList = [];
        console.log('pokerList', pokerList);
        for(var i=0;i<pokerList.length;i++){
            const id = pokerList[i].actionCode
            if (id === 7) {
                // this.qishouhula(pokerList[i])
                continue 
            }
            let chidepai = pokerList[i].actionCards ? pokerList[i].actionCards[0].count : null
            if (pokerList[i].actionCard) {
                this.winPoker(pokerList[i].actionCard.suitType,pokerList[i].actionCard.count,pokerList[i].actionCard.cardIndex,GameData.winPokerCode[pokerList[i].actionCode], chidepai);
            }
        }
    }

    public winPoker(flower:string,value:string,id:number,type:string, chidepai?: number){
        if (type === 'hu') {
        return
        }
        if(flower&&value){
            if(type!="peng"&&type!="chi"){
                //清除已经碰了的相同牌   
                for(var i=0;i<this.winPokerList.length;i++){
                    if(this.winPokerList[i].name == flower+value+"_peng"){                        
                        this.winPokerBg.removeChild(this.winPokerList[i]);
                        this.winPokerList.splice(i,1);
                    }
                }
            }

            let lsobj:any;
            if(this.SeatType == 1){
                lsobj = new winRight();
            }
            if(this.SeatType == 2){
                lsobj = new winTop();
            }
            if(this.SeatType == 3){
                lsobj = new winLeft();
            }
            lsobj.name = flower+value+"_"+type;
            if (type === 'bu_card') {
                lsobj.setContent("X"+flower+"_"+value+"_png",'gonggang', chidepai);
            } else {
                lsobj.setContent("X"+flower+"_"+value+"_png",type, chidepai);                
            }
            this.winPokerList.push(lsobj);
            this.winPokerBg.addChild(lsobj);
            this.updateSize();  
            console.log('type', type);
            console.log('chidepai', chidepai);
            if(type == "peng"){
                this.deleteAppointPoker(flower,value,2);//删除相同手牌2张
            }else if(type == "gang"||type == "gonggang"||type == "angang" || type == 'bu_card'){
                this.deleteAppointPoker(flower,value);//删除所有相同手牌
            }else if(type == "chi"){
                if (chidepai === parseInt(value)) {
                    this.deleteAppointPoker(flower,chidepai - 1 + '',1);
                    this.deleteAppointPoker(flower,chidepai + 1 + '',1);
                } else if (chidepai === parseInt(value) - 1) {
                    this.deleteAppointPoker(flower,chidepai + 1 + '',1);
                    this.deleteAppointPoker(flower,chidepai + 2 + '',1);
                } else if (chidepai === parseInt(value) + 1) {
                    this.deleteAppointPoker(flower,chidepai - 1 + '',1);
                    this.deleteAppointPoker(flower,chidepai - 2 + '',1);
                }
            }           
        }
    }

    // //赢牌-碰牌/杠(花色，值，ID，赢牌的碰杠类型)
    // public winPoker(flower:string,value:string,id:number,type:string){
    //     if(flower&&value){        
    //         if(type!="peng"&&type!="chi"){
    //             //清除已经碰了的相同牌  
    //             for(var i=0;i<this.winPokerList.length;i++){
    //                 if(this.winPokerList[i].name == flower+value+"_peng"){
    //                     this.winPokerBg.removeChild(this.winPokerList[i]);
    //                     this.winPokerBg.width = (this.winPokerList[i].width+10)*this.winPokerBg.numChildren;
    //                     this.winPokerList.splice(i,1);
    //                 }
    //             }
    //         }
            
    //         let lsobj:any;
    //         if(this.SeatType == 1){
    //             lsobj = new winRight();
    //         }
    //         if(this.SeatType == 2){
    //             lsobj = new winTop();
    //         }
    //         if(this.SeatType == 3){
    //             lsobj = new winLeft();
    //         }
    //         lsobj.name = flower+value+"_"+type;
    //         lsobj.setContent("X"+flower+"_"+value+"_png",type);
    //         this.winPokerList.push(lsobj);
    //         this.winPokerBg.addChild(lsobj);            

    //         if(type == "peng"){
    //             this.deleteAppointPoker(flower,value,2);//删除相同手牌2张
    //         }else if(type == "gang"||type == "gonggang"||type == "angang"){
    //             this.deleteAppointPoker(flower,value);//删除所有相同手牌
    //         }else if(type == "chi"){

    //         }
    //         this.updateSize(); 
    //         this.pokerArrayAscending();            
    //     }        
    // }
    //刷新赢牌列表-碰牌/杠(花色，值，ID，赢牌的碰杠类型)
    /*public winPokerUpdateList(pokerList:Array<any>){  
        for(var i=this.winPokerBg.numChildren;i>0;i--){
            this.winPokerBg.removeChildAt(i-1);
        }
        this.winPokerList = [];
        for(var i=0;i<pokerList.length;i++){
            this.winPoker(pokerList[i].actionCard.suitType,pokerList[i].actionCard.count,pokerList[i].actionCard.cardIndex,GameData.winPokerCode[pokerList[i].actionCode]);
        }
    }*/

    /*public winPoker(flower:string,value:string,id:number,type:string){
        if(flower&&value){
            if(type!="peng"&&type!="chi"){
                //清除已经碰了的相同牌   
                for(var i=0;i<this.winPokerList.length;i++){                    
                    if(this.winPokerList[i].name == flower+value+"_peng"){                        
                        this.winPokerBg.removeChild(this.winPokerList[i]);
                        this.winPokerList.splice(i,1);
                    }
                }
            }

            let lsobj:any;
            if(this.SeatType == 1){
                lsobj = new winRight();
            }
            if(this.SeatType == 2){
                lsobj = new winTop();
            }
            if(this.SeatType == 3){
                lsobj = new winLeft();
            }
            lsobj.name = flower+value+"_"+type;
            lsobj.setContent("X"+flower+"_"+value+"_png",type);
            this.winPokerList.push(lsobj);
            this.winPokerBg.addChild(lsobj);
            this.updateSize();  

            if(type == "peng"){
                this.deletePoker(2);
            } 
            if(type == "gang"){
                this.deletePoker(3);
            }    
            if(type == "gonggang"){
                this.deletePoker(1);
            }        
            if(type == "angang"){
                this.deletePoker(1);
            }           
        }
    } */
}